package com.example.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class InterceptorExample implements HandlerInterceptor {
    // 在目标资源方法执行前执行，返回true就是放行，false就是不放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("PreHandle ...");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
    // 目标资源方法执行后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("PostHandle ...");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
    // 最后执行，一般是使用了视图，在视图渲染后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("AfterCompletion ...");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}